home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / macfreeze / directives.py next >
Encoding:
Text File  |  2000-06-23  |  1.1 KB  |  40 lines

  1. import re
  2. import os
  3.  
  4. # The regular expression for freeze directives. These are comments with the
  5. # word macfreeze immedeately followed by a colon, followed by a directive,
  6. # followed by argument(s)
  7. #
  8. # The directives supported are
  9. # include - Include a module or file
  10. # exclude - Exclude a module
  11. # path - Add sys.path entries. Relative paths are relative to the source file.
  12. #
  13. # See the macfreeze.py main program for a real live example.
  14. #
  15. DIRECTIVE_RE=r'^\s*#\s*macfreeze:\s*(\S*)\s*(.*)\s*$'
  16. REPROG=re.compile(DIRECTIVE_RE)
  17.  
  18. def findfreezedirectives(program):
  19.     extra_modules = []
  20.     exclude_modules = []
  21.     extra_path = []
  22.     progdir, filename = os.path.split(program)
  23.     fp = open(program)
  24.     for line in fp.readlines():
  25.         match = REPROG.match(line)
  26.         if match:
  27.             directive = match.group(1)
  28.             argument = match.group(2)
  29.             if directive == 'include':
  30.                 extra_modules.append(argument)
  31.             elif directive == 'exclude':
  32.                 exclude_modules.append(argument)
  33.             elif directive == 'path':
  34.                 argument = os.path.join(progdir, argument)
  35.                 extra_path.append(argument)
  36.             else:
  37.                 print '** Unknown directive', line
  38.     return extra_modules, exclude_modules, extra_path
  39.     
  40.